/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.NursingProject;

@JsonApiResourceRepository(NursingProject.class)
@Component
public class NursingProjectRepository extends BaseRepository<NursingProject> {
	private static Log logger = LogFactory.getLog(NursingProjectRepository.class);

	@Autowired
	private NursingService nursingService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(NursingProject.class);
	}

	@Override
	@JsonApiFindAll
	public Iterable<NursingProject> findAll(final QueryParams requestParams) {
		List data = new ArrayList();
		data = (List) super.findAll(requestParams);
		return data;
	}

	@Override
	@JsonApiSave
	public NursingProject save(NursingProject project) {
		if (project.getId() == null) {
			project = super.save(project);
		} else {
			if (project.getDelStatus() == 1) {// 删除
			// int deleteFlag = nursingService.checkProjectRelations(project);
			// if(deleteFlag>0){
			// project.setErrcode(8);// no delete
			// throw new BusinessException(null,
			// CommonConstants.ERROR_CODE.NO_DELETE.label(), null);
			// }else{
			// }
				nursingService.deleteNursingProject(project);
			} else {
				if (project.getUpdateFlag() != null && !"".equals(project.getUpdateFlag())) {
					List<Long> itemIds = new ArrayList<>();// 去掉的projectitem id
					String[] ids = project.getUpdateFlag().split(",");
					for (String id : ids) {
						if (id != null && !"".equals(id) && id.matches("\\d+")) {
							itemIds.add(Long.parseLong(id));
						}
					}
					int deleteItemFlag = 0;
					if (itemIds.size() > 0) {
						deleteItemFlag = nursingService.checkProjectItemRelations(
								project.getCustomer(), itemIds, CommonUtil.getCurrTimes());// 验证这些projectItem与计划是否有关联
					}
					if (deleteItemFlag > 0) {
						project.setErrcode(8);// no delete
						throw new BusinessException(null,
								CommonConstants.ERROR_CODE.NO_DELETE.label(), null);
					}
				}

				project = super.save(project);
			}
		}
		return project;
	}

}
